package edu.columbia.doodle.component
{
	import edu.columbia.doodle.skin.HScrollButtonSkin;
	
	import flash.display.GradientType;
	import flash.display.Sprite;
	
	import spark.components.BorderContainer;
	import spark.components.Button;
	import spark.components.Label;
	import spark.components.SkinnableContainer;
	import spark.components.supportClasses.SkinnableComponent;
	
	public class TagContainer extends SkinnableContainer
	{
		private var _borderRadius:Number;
		private var _labelWidth:Number;
		private var _labelHeight:Number;
		private var _containerWidth:Number;
		private var _containerHeight:Number;
		private var _borderThickness:Number;
		private var _borderColor:uint;
		private var _backgroundColor:uint;
		private var _borderAlpha:Number;
		private var _backgroundAlpha:uint;
		private var _label:Label;
		private var _container:BorderContainer;
		
		private var _prevButton:Button;
		private var _moreButton:Button;
		
		public function TagContainer()
		{
			super();
			_borderRadius = 5;
			_labelWidth = 150;
			_labelHeight = 50;
			_containerWidth = 700;
			_containerHeight = 130;
			_borderThickness = 5;
			_borderColor = 0xBBBBBB;
			_borderAlpha = 0.9;
			_backgroundColor = 0xeeeeee;
			_backgroundAlpha = 1;
			_label = new Label();
			_container = new BorderContainer();
			_container.visible = false;
			_prevButton = new Button();
			_prevButton.x = borderRadius + 755;
			_prevButton.top = borderRadius + 10;
			_prevButton.label = "Prev";
			addElement(_prevButton);
			_moreButton = new Button();
			_moreButton.x = borderRadius + 755;
			_moreButton.bottom = borderRadius + 10;
			_moreButton.label = "More";
			addElement(_moreButton);
			drawBorder(this);
		    addElement(_label);
			addElement(_container);
//			var border:Sprite = new Sprite();
//			addChild(border);
		}
		
		
		public function get borderRadius():Number
		{
			return _borderRadius;
		}
		
		public function set borderRadius(borderRadius:Number):void
		{
			_borderRadius = borderRadius;
			drawBorder(this);
		}
		
		public function get labelWidth():Number
		{
			return _labelWidth;
		}
		
		public function set labelWidth(labelWidth:Number):void
		{
			_labelWidth = labelWidth;
			drawBorder(this);
			this.width = _labelWidth + _containerWidth;
		}
		
		public function get labelHeight():Number
		{
			return _labelHeight;
		}
		
		public function set labelHeight(labelHeight:Number):void
		{
			_labelHeight = labelHeight;
			drawBorder(this);
			this.height = _labelHeight > _containerHeight ? _labelHeight : _containerHeight;
		}
		
		public function get containerWidth():Number
		{
			return containerWidth;
		}
		
		public function set containerWidth(containerWidth:Number):void
		{
			_containerWidth = containerWidth;
			drawBorder(this);
			this.width = _labelWidth + _containerWidth;
		}
		
		public function get containerHeight():Number
		{
			return _containerHeight;
		}
		
		public function set containerHeight(containerHeight:Number):void
		{
			_containerHeight = containerHeight;
			drawBorder(this);
			this.height = _labelHeight > _containerHeight ? _labelHeight : _containerHeight;
		}
		
		public function get borderThickness():Number
		{
			return _borderThickness;
		}
		
		public function set borderThickness(borderThickness:Number):void
		{
			_borderThickness = borderThickness;
			drawBorder(this);
		}
		
		public function get borderColor():uint
		{
			return _borderColor;
		}
		
		public function set borderColor(borderColor:uint):void
		{
			_borderColor = borderColor;
			drawBorder(this);
		}
		
		public function get borderAlpha():Number
		{
			return _borderAlpha;
		}
		
		public function set borderAlpha(borderAlpha:Number):void
		{
			_borderAlpha = borderAlpha;
			drawBorder(this);
		}
		
		public function get backgroundColor():uint
		{
			return _backgroundColor;
		}
		
		public function set backgroundColor(backgroundColor:uint):void
		{
			_backgroundColor = backgroundColor;
			drawBorder(this);
		}
		
		public function get label():Label
		{
			return _label;
		}
		
		public function set label(label:Label):void
		{
			this.removeElement(_label);
			_label = label;
			if (_label != null) {
				_label.left = _borderThickness;
				_label.top = _borderThickness;
				_label.buttonMode = true;
				addElement(_label);
			}
		}
		
		public function get container():BorderContainer
		{
			return _container;
		}
		
		public function set container(container:BorderContainer):void
		{
			this.removeElement(_container);
			_container = container;
			
			if (_container != null) {
				_container.x = _labelWidth;
				addElement(_container);
			}
		}
		
		public function get prevButton():Button
		{
			return _prevButton;
		}
		
		public function set prevButton(prevButton:Button):void
		{
			removeElement(_prevButton);
			_prevButton = prevButton;
			if (_prevButton != null) {
				_prevButton.right = borderRadius + 5;
				_prevButton.top = borderRadius + 10;
				_prevButton.label = "Prev";
				addElement(_prevButton);
			}
			
		}
		
		public function get moreButton():Button
		{
			return _moreButton;
		}
		
		public function set moreButton(moreButton:Button):void
		{
			removeElement(_moreButton);
			_moreButton = moreButton;
			if (_moreButton != null) {
				_moreButton.right = borderRadius + 5;
				_moreButton.bottom = borderRadius + 10;
				_moreButton.label = "More";
				addElement(_moreButton);
			}
		}
		
		private function drawBorder(sprite:Sprite):void
		{
			sprite.graphics.clear();
			sprite.graphics.beginFill(_backgroundColor);
			sprite.graphics.lineStyle(_borderThickness, _borderColor, _borderAlpha);
			//Start
			sprite.graphics.moveTo(_borderRadius, 0);
			sprite.graphics.lineTo(_labelWidth + _containerWidth - _borderRadius, 0);
			//Right-top corner
			sprite.graphics.curveTo(_labelWidth + _containerWidth, 0, _labelWidth + _containerWidth, _borderRadius);
			sprite.graphics.lineTo(_labelWidth + _containerWidth, _containerHeight - _borderRadius);
			//Right-bottom corner
			sprite.graphics.curveTo(_labelWidth + _containerWidth, _containerHeight, _labelWidth + _containerWidth - _borderRadius, _containerHeight);
			sprite.graphics.lineTo(_labelWidth + _borderRadius, _containerHeight);
			//Mid-bottom corner
			sprite.graphics.curveTo(_labelWidth, _containerHeight, _labelWidth, _containerHeight - _borderRadius);
			sprite.graphics.lineTo(_labelWidth, _labelHeight + _borderRadius);
			//Mid-center corner
			sprite.graphics.curveTo(_labelWidth, _labelHeight, _labelWidth - borderRadius, _labelHeight);
			sprite.graphics.lineTo(_borderRadius, _labelHeight);
			//Left-bottom corner
			sprite.graphics.curveTo(0, _labelHeight, 0, _labelHeight - _borderRadius);
			sprite.graphics.lineTo(0, _borderRadius);
			//Left-top corner
			sprite.graphics.curveTo(0, 0, _borderRadius, 0);
			sprite.graphics.endFill();
		}
	}
}